<?php
namespace app\admin\controller;
use app\api\controller\Task;
use app\common\service\Bs;
use app\common\service\Timer;
class AuthRole extends Base
{
    public function index()
    {
        $page = input('page', 1);
        $data = model('AuthRole')->list_data(input(), $page, 10);
        $this->assign('data', $data);
        return $this->fetch();
    }
    function form()
    {
        $id = input('id', 0);
        if ($id) {
            $this->assign('data', model('AuthRole')->fetch_data($id));
        }
        return $this->fetch();
    }
    public function auth_rule(){
        if (request()->isPost()) {
            $param = input('post.');
            if(empty($param['id'])){
                 data_return('非法操作', [], -1);
            }
            if(empty($param['ids'])){
                 data_return('请选择权限', [], -1);
            }
            $data=[];
            $data['rule_ids']=implode(',',$param['ids']);
            $res=model('AuthRole')->where('id',$param['id'])->update($data);
            data_return('操作成功', $res);
        }else{
            $id = input('id', 0);
            $uriInfo = model('AuthRole')->fetch_data($id);
            $uriInfo['rule_ids']=explode(',',$uriInfo['rule_ids']);
            
            $query = model('AuthRule')->list_data(array('status'=>1));
            $list = list_for_level($query);
            $this->assign('list',$list);
            $this->assign('info',$uriInfo);
            return $this->fetch();
        }
        
    }
    public function action_data()
    {
        if (!request()->isPost()) {
            data_return('非法操作', [], -1);
        }
        $param = input('post.');
        if (!empty($param['type']) && $param['type'] == 'setStatus') {
            $res = model('AuthRole')->update_data($param);
        } elseif (!empty($param['type']) && $param['type'] == 'del') {
            $res = model('AuthRole')->del_data($param['id']);
        } else {
            if (!empty($param['id'])) {
                $res = model('AuthRole')->update_data($param);
            } else {
                unset($param['id']);
                $res = model('AuthRole')->create_data($param);
            }
        }
        data_return('操作成功', $res);
    }
}